package com.uls.ldcm.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.uls.common.utils.DateUtils;
import com.uls.common.utils.SecurityUtils;
import com.uls.ldcm.domain.Laborers;
import com.uls.ldcm.domain.Salarypayments;
import com.uls.ldcm.domain.Salaryschemes;
import com.uls.ldcm.mapper.LaborersMapper;
import com.uls.ldcm.mapper.SalarypaymentsMapper;
import com.uls.ldcm.mapper.SalaryschemesMapper;
import com.uls.ldcm.service.ILaborersService;
import com.uls.ldcm.service.ISalarypaymentsService;
import com.uls.ldcm.service.ISalaryschemesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.uls.ldcm.mapper.ContractsMapper;
import com.uls.ldcm.domain.Contracts;
import com.uls.ldcm.service.IContractsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 合同信息Service业务层处理
 * 
 * @author gw
 * @date 2024-10-31
 */
@Service
public class ContractsServiceImpl implements IContractsService 
{
    @Autowired
    private ContractsMapper contractsMapper;


    @Autowired
    private ILaborersService laborersService;

    @Autowired
    private ISalarypaymentsService salarypaymentsService;


    @Autowired
    private ISalaryschemesService salaryschemesService;


    /**
     * 查询合同信息
     * 
     * @param contractId 合同信息主键
     * @return 合同信息
     */
    @Override
    public Contracts selectContractsByContractId(Long contractId)
    {
        return contractsMapper.selectContractsByContractId(contractId);
    }

    /**
     * 查询合同信息列表
     * 
     * @param contracts 合同信息
     * @return 合同信息
     */
    @Override
    public List<Contracts> selectContractsList(Contracts contracts)
    {
        return contractsMapper.selectContractsList(contracts);
    }

    /**
     * 新增合同信息
     * 
     * @param contracts 合同信息
     * @return 结果
     */

    @Transactional
    @Override
    public int insertContracts(Contracts contracts)
    {
        contracts.setCreatedAt(DateUtils.getNowDate());
        contracts.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserName());
        int result=contractsMapper.insertContracts(contracts);
        Laborers laborers = new Laborers();
        laborers.setName(contracts.getName());
        laborers.setContractId(contracts.getContractId());
        laborersService.insertLaborers(laborers);
        return result;
    }

    /**
     * 修改合同信息
     * 
     * @param contracts 合同信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateContracts(Contracts contracts)
    {
        contracts.setCreatedAt(DateUtils.getNowDate());
        contracts.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserName());
/*        List<Salarypayments> salarypaymentsList = salarypaymentsService.selectSalarypaymentsListByContractId(contracts.getContractId());
        for(Salarypayments salarypayments1:salarypaymentsList){
            Salaryschemes salaryschemes = new Salaryschemes();
            salaryschemes.setSalaryschemeId(contracts.getSalaryschemeId());
            List<Salaryschemes> salaryschemes1 = salaryschemesService.selectSalaryschemesList(salaryschemes);
            salarypayments1.setAmount(salaryschemes1.get(0).getBasicSalary().add(salaryschemes1.get(0).getOvertimePay()).add(salaryschemes1.get(0).getBonus()).add(salaryschemes1.get(0).getAllowances()));
            salarypaymentsService.updateSalarypayments(salarypayments1);
        }*/
        return contractsMapper.updateContracts(contracts);
    }

    /**
     * 批量删除合同信息
     * 
     * @param contractIds 需要删除的合同信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteContractsByContractIds(Long[] contractIds)
    {
        for(Long contractId:contractIds){
            Laborers laborers = new Laborers();
            laborers.setContractId(contractId);
            List<Laborers> laborersList = laborersService.selectLaborersList(laborers);
            laborersService.deleteLaborersById(laborersList.get(0).getId());
        }
        return contractsMapper.deleteContractsByContractIds(contractIds);
    }

    /**
     * 删除合同信息信息
     * 
     * @param contractId 合同信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteContractsByContractId(Long contractId)
    {
        Laborers laborers = new Laborers();
        laborers.setContractId(contractId);
        List<Laborers> laborersList = laborersService.selectLaborersList(laborers);
        for(Laborers laborers1:laborersList){
            laborersService.deleteLaborersById(laborers1.getId());
        }
        return contractsMapper.deleteContractsByContractId(contractId);
    }
}
